<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="标签名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入标签名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>

    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table
      v-loading="loading"
      :data="list.slice((pageNum-1)*pageSize,pageNum*pageSize)"
      style="width: 100%;"
    >
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="标签名称" align="center" prop="name"/>
      <el-table-column label="排序号" align="center" prop="sortNum"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleEdit(scope.row)"
            v-hasPermi="['monitor:online:forceLogout']"
          >编辑
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:post:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="pageNum" :limit.sync="pageSize"/>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文档标题" prop="title">
          <el-input v-model="form.name" placeholder="请输入文档标题"/>
        </el-form-item>
        <el-form-item label="排序号" prop="sortNum">
          <el-input-number v-model="form.sortNum" controls-position="right" :min="0" :max="99999999" placeholder="越大越靠前"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {listTags, saveTag, delTags} from "@/api/official/tags"
import {saveDoc} from "@/api/official/docs";

export default {
  name: "Docs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 显示搜索条件
      showSearch: true,
      pageNum: 1,
      pageSize: 10,
      open: false,
      title: "",
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        name: undefined,
        sortNum: 0
      },
      // 表单校验
      rules: {
        name: [
          {required: true, message: "标签名称不能为空", trigger: "blur"}
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      listTags(this.queryParams).then(response => {
        this.list = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 表单重置
    reset() {
      this.form = {
        name: undefined,
        sortNum: 0
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 编辑 */
    handleEdit(row) {
      this.reset()
      this.title = "编辑标签"
      // 深拷贝，避免直接绑定表格行对象导致联动修改
      this.form = JSON.parse(JSON.stringify(row))
      this.open = true
    },
    handleDelete(row){
      this.$modal.confirm('是否确认删除标签名称为"' + row.name + '"的数据项？').then(function () {
        return delTags(row.id)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    submitForm(){
      this.$refs["form"].validate(valid => {
        if (valid) {
          saveTag(this.form).then(response => {
            console.log('response',response)
            if (this.form.id != undefined) {
              this.$modal.msgSuccess("修改成功")
            } else {
              this.$modal.msgSuccess("新增成功")
            }

            this.open = false
            this.reset()
            this.getList()
          })

        }
      })
    },
    handleAdd(){
      this.reset()
      this.title = "添加标签"
      this.open = true
    }
  }
}
</script>

